﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;

namespace SGRapidForm.Permission.Component
{
    public class ModuleComponent : ComponentBase
    {

        /// <summary>
        /// 添加一位新用户
        /// </summary>
        /// <param name="UserDetails">一位用户</param>
        /// <returns>是否添加成功</returns>
        public bool AddModule(ModuleEntity module)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Module_Add", module) > 0);
        }
        /// <summary>
        /// 删除一位模块
        /// </summary>
        /// <param name="keyid">用户的Keyid</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteModule(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Module_Delete", null, keyid) > 0);
        }

        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="keyids">keyids</param>
        /// <returns>批量删除是否成功</returns>
        public bool BulkDeleteModule(string keyids)
        {
            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Module_BulkDelete", null, keyids) == count);
        }
        /// <summary>
        /// 根据Keyid获取一位用户
        /// </summary>
        /// <param name="keyid">用户的Keyid</param>
        /// <returns>一位用户</returns>
        public ModuleEntity GetModuleByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<ModuleEntity>("sgrf_permission_Module_GetModuleByKeyid", null, keyid);
        }
        /// <summary>
        /// 根据RoleKeyid获取模块
        /// </summary>
        /// <param name="keyid">岗位的RoleKeyid</param>
        /// <returns>模块</returns>
        public List<ModuleEntity>  GetModuleByRoleKeyid(string Rolekeyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<ModuleEntity>("sgrf_permission_RoleModule_GetModuleByRoleKeyid", null, Rolekeyid);
        }
        /// <summary>
        /// 更新一位用户
        /// </summary>
        /// <param name="UserDetails">一位用户</param>
        /// <returns>是否更新成功</returns>
        public bool UpdateModule(ModuleEntity module)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Module_Update", module) > 0);
        }
        /// <summary>
        /// 查询所有模块信息 
        /// </summary>
        /// <returns>模块信息集合</returns>
        public List<ModuleEntity> Module_GetAll()
        {
            return FishBLLHelper.CallSpGetDataItemList<ModuleEntity>("sgrf_permission_Module_GetAll", null);
        }

        /// <summary>
        /// 进行过滤、排序、分页查询用户
        /// </summary>
        /// <param name="startRecord">开始记录索引</param>
        /// <param name="maxRecords">每页记录数</param>
        /// <param name="sortColumns">排序列</param>
        /// <param name="filterConditions">过滤条件SQL</param>
        /// <returns>用户集合</returns>
        public List<ModuleEntity> PagingGetModules(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            VerifySortColumns(sortColumns);
            if (string.IsNullOrWhiteSpace(sortColumns))
            {
                sortColumns = "CreateDate DESC";
            }

            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();

            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ModuleName", DbType.String, " AND ModuleName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ModuleCode", DbType.String, " AND ModuleCode like '%{0}%' "));

            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ModuleNameEn", DbType.String, " AND ModuleNameEn like '%{0}%' "));
            filterConditions = sql.ToString();

            return FishBLLHelper.CallSpGetDataItemList<ModuleEntity>("sgrf_permission_Module_PagingGetUsers", null, startRecord, maxRecords, sortColumns, filterConditions);
        }

        /// <summary>
        /// 验证排序参数是否包含非法字符，有则抛出异常
        /// </summary>
        /// <param name="sortColumns"></param>
        private void VerifySortColumns(string sortColumns)
        {
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');

            foreach (String columnName in columnNames)
            {

                switch (columnName.Trim().ToLowerInvariant())
                {

                    case "modulename":
                    case "modulecode":
                    case "createdate":
                    case "modulenameen":
                    case "":
                        break;
                    default:
                        throw new ArgumentException("SortColumns contains an invalid column name.");
                }
            }
        }

        /// <summary>
        /// 进行过滤、排序、分页查询用户记录数
        /// </summary>
        /// <param name="filterConditions">过滤条件</param>
        /// <returns>记录数</returns>
        public int PagingGetModuleCount(String filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ModuleName", DbType.String, " AND ModuleName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ModuleCode", DbType.String, " AND ModuleCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ModuleNameEn", DbType.String, " AND ModuleNameEn like '%{0}%' "));
            filterConditions = sql.ToString();

            return Convert.ToInt32(FishBLLHelper.CallSpExecuteScalar("sgrf_permission_Module_PagingGetModuleCount", null, filterConditions));
        }
    }
}
